#include "adt.h"
#include <stdio.h>
void* pcmToWav(const void *pcm, unsigned int pcmlen, unsigned int *wavlen);
char* encode_to(char *input);
int main(int argc, char** argv)
{
   short* buffer;
   void* encoder;
   char* input;
   int bsize, ret;
   int need_hamming = 0;
   config_encoder_t config_encoder;
   config_encoder.max_strlen = 128;
   config_encoder.freq_type = FREQ_TYPE_MIDDLE;
   config_encoder.sample_rate = 44100;
   config_encoder.error_correct = 1;
   config_encoder.error_correct_num = 4;
   config_encoder.group_symbol_num = 10;
    printf("%d", argc);
   if (argc < 3){
       printf("please input encode string and wav file name, eg:\n");
       printf("ADTCommand abcdefg  abc.wav\n");
       return 0;
   }
    if(argc == 4){
        // printf("argv[2]=%s", argv[3]);
        if(strcmp(argv[3], "hamming")==0){
            need_hamming = 1;
        }
    }
  /* create encoder handle */
   encoder = encoder_create(&config_encoder);
   if(encoder == NULL){
        printf("create encoder handle error !\n");  
        return -1; 
    }
  /* get size of the buffer to be malloced */
   bsize = encoder_getoutsize(encoder);
//    printf("bsize=%d\n", bsize);
   buffer = (short*)malloc(bsize);
   if(buffer == NULL){
        printf("alloc buffer error!\n");
        return -1; 
    }
    /* set the input string to be transmitted */
    if(need_hamming == 1){
        input = encode_to(argv[1]);
        printf("need_hamming.....\n");
    }else{
        input = (char *)malloc(strlen(argv[1])+1);
        strcpy(input, argv[1]);
    }

   ret = encoder_setinput(encoder, input);
   if(ret == RET_ENC_ERROR){
        printf("encoder set input error!\n");
        return -1; 
    }
    /* encoding loop */
    ret = RET_ENC_NORMAL;
    int n = 0;
    void *outBuff[300];
    while(ret != RET_ENC_ERROR && ret != RET_ENC_END){
      /* get the encoded pcm data */
        ret = encoder_getpcm(encoder, buffer);
        outBuff[n] =  (void *)malloc(bsize);
        memcpy(outBuff[n], buffer, bsize);
        n++;
        /* transfer pcm data to DAC */
    }
    void *wavbuff = (void *)malloc(bsize * n);
    int i = 0;
    for(i=0; i<n; i++){
        memcpy(wavbuff+i*bsize, outBuff[i], bsize);
        free(outBuff[i]);
    }
    unsigned int wavLen;  
    void *wav = pcmToWav(wavbuff, bsize*n, &wavLen);
    FILE *fwav = fopen(argv[2], "wb");  
    fwrite(wav, 1, wavLen, fwav);  
    fclose(fwav);  
    free(wav);

    FILE *pcmFile = fopen("abc.pcm", "wb");
    fwrite(wavbuff, 1, n*bsize, pcmFile);  
    fclose(pcmFile);  

   if(ret == RET_ENC_ERROR){
        printf("encoder error occured!\n");
        return -1; 
    }
    /* free encoder handle */
    encoder_destroy(encoder);
    return 0; 
}

void* pcmToWav(const void *pcm, unsigned int pcmlen, unsigned int *wavlen)  
{  
    //44字节wav头  
    void *wav = malloc(pcmlen + 44);  
    //wav文件多了44个字节  
    *wavlen = pcmlen + 44;  
    //添加wav文件头  
    memcpy(wav, "RIFF", 4);  
    *(int *)((char*)wav + 4) = pcmlen + 36;  
    memcpy(((char*)wav + 8), "WAVEfmt ", 8);  
    *(int *)((char*)wav + 16) = 16;  
    *(short *)((char*)wav + 20) = 1;  
    *(short *)((char*)wav + 22) = 1;  
    *(int *)((char*)wav + 24) = 44100;  
    *(int *)((char*)wav + 28) = 44100;  
    *(short *)((char*)wav + 32) = 16 / 8;  
    *(short *)((char*)wav + 34) = 16;  
    strcpy((char*)((char*)wav + 36), "data");  
    *(int *)((char*)wav + 40) = pcmlen;  
  
    //拷贝pcm数据到wav中  
    memcpy((char*)wav + 44, pcm, pcmlen);  
    return wav;  
}  